function isLogin(cbFinal) {
  console.log('isLogin======>in')
  return new Promise(function(resolve, reject) {
    window.bridge.callHandler('isLogin', {}, function(res) {
      if (res.result === '1') {
        console.log('前置登录')
        getUserInfo()
        resolve({ userIsLogin: true })
      } else {
        console.log('前置未登录')
        login()
        reject({ userIsLogin: false })
      }
    })
  })
}
function login() {
  return new Promise((resolve, reject) => {
    window.bridge.callHandler('login', {}, function(response) {
      if (typeof response == 'string') {
        response = JSON.parse(response)
      }
      //去到登录页面
      if (res.result == 0) {
        console.log('登录失败')
      } else {
        console.log('登录成功')
        getUserInfo()
      }
      console.log('登录页面：login')
      resolve(response)
    })
  })
}

function getUserInfo() {
  return new Promise(function(resolve, reject) {
    window.bridge.callHandler('getUserInfo', {}, function(res) {
      if (typeof res == 'string') {
        res = JSON.parse(res)
      }
      console.log('GetUserInfo res =======>', res)
      if (res.status == '1') {
        console.log('未认证')
        userAuthenticate()
        resolve(res)
      } else {
        console.log('认证')
        let isMyCompanysData = {
          userid: res.userid,
          mobile: res.userphone
        }
        myCompanys(isMyCompanysData)
        reject('Get user info error.')
      }
    })
  })
}
function userAuthenticate() {
  return new Promise((resolve, reject) => {
    window.bridge.callHandler('userAuthenticate', {}, function(response) {
      if (typeof response == 'string') {
        response = JSON.parse(response)
      }
      console.log('实名认证：userAuthenticate', response)
      //resolve(response)
    })
  })
}
function preCheckStep() {
  isLogin()
}
//判断有无企业
function myCompanys(isMyCompanysData) {
  let v = this
  getHttp('101101', isMyCompanysData).then(res => {
    //我的企业-关联企业信息查询
    if (res.totnum == '0' || res.totnum == '' || res.totnum == undefined) {
      //没有企业，去创建企业
      console.log('没有企业，去创建企业')
      companyList()
    } else {
      //有企业,判断有没有认证
      console.log('有企业,判断有没有认证')
      let returnData = res.custinf.filter(certificationCom()) //得到01状态的认证企业当前整个对象
      console.log('得到01状态的认证企业当前整个对象', returnData)
      if (returnData == undefined || returnData.length == 0) {
        companyList()
      } else {
        console.log('没有认证')
      }
    }
  })
}
function companyList() {
  return new Promise((resolve, reject) => {
    window.bridge.callHandler('companyList', {}, function(response) {
      if (typeof response == 'string') {
        response = JSON.parse(response)
      }
      pushWebpage({
        title: '',
        close: '0',
        back: '0',
        url: `static/leyeapp/home/myEnterprise/myEnterprise.html`
      })
      console.log('跳到贷款：companyList')
      //resolve(response)
    })
  })
}
function pushWebpage(obj) {
  return new Promise((resolve, reject) => {
    window.bridge.callHandler(
      'pushWebpage',
      {
        pageUrl: obj.url,
        title: obj.title,
        success_close_current_view: obj.close,
        full_screen: obj.full ? String(obj.full) : '1',
        has_android_back_func: obj.back ? String(obj.back) : '1',
        default_status_style: obj.style ? String(obj.style) : '0',
        has_close_button: obj.hasCloseBtn ? String(obj.hasCloseBtn) : '0',
        need_token: obj.need_token ? obj.need_token : '0'
      },
      function(response) {
        if (typeof response == 'string') {
          response = JSON.parse(response)
        }
        console.log('pushMessage')
        resolve(response)
      }
    )
  })
}
//循环遍历查回来的企业有没有认证过企业
function certificationCom(states) {
  return states.custauthctstat == '01'
}
function getHttp(tscode, body) {
  console.log(process.env.NODE_ENV)
  return new Promise(function(resolve, reject) {
    window.bridge.callHandler(
      'serverRequest',
      {
        transcode: 'ucspss' + tscode,
        body: body
      },
      function(response) {
        if (typeof response == 'string') {
          response = JSON.parse(response)
        }
        console.log('发收数据。。。', tscode, body)
        console.log('接收数据', response, tscode)
        if (response.header.resultcode == '000000') {
          resolve(response.body)
        } else {
          Vue.prototype.$showModal({
            msg: response.header.resultdesc
          })
          reject(response)
        }
      }
    )
  })
}
export { preCheckStep }
